﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoVanHocTapData.Concretes;
using CoVanHocTapProject.Infrastructure;
using CoVanHocTapData.Entities;

namespace CoVanHocTapProject.Controllers
{
    public class TheoDoiSinhVienController : Controller
    {
        SinhVienRepository sinhVienRepo = new SinhVienRepository();
        TinChiDauRaRepository tcDauRaRepo = new TinChiDauRaRepository();
        ChuanDauRaRepository chuanDauRaRepo = new ChuanDauRaRepository();
        MonHocRepository monHocRepo = new MonHocRepository();
        QuyDinhLuanVan_TTTNRepository qdLVTTTNRepo = new QuyDinhLuanVan_TTTNRepository();
        //
        // GET: /TheoDoiSinhVien/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /TheoDoiSinhVien/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /TheoDoiSinhVien/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /TheoDoiSinhVien/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TheoDoiSinhVien/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /TheoDoiSinhVien/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TheoDoiSinhVien/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /TheoDoiSinhVien/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        [HttpGet]
        public ActionResult TheoDoiSinhVien()
        {

            var dsmon1289 = new KetQuaHocRepository().ds4mon("081289T");
            var diemtrb = new KetQuaHocRepository().diemtrb4mon(dsmon1289);

            #region Khai báo chung
            TheoDoiSinhVienModel model = new TheoDoiSinhVienModel();

            //string maSVdangXet = "081301T";
            string maSVdangXet = Publics.sinhVienDangNhap.MSSV;

            string maNganh = new NganhDaoTaoRepository().LayMaNganhTuMaSV(maSVdangXet);
            string maHeDaoTao = new HeDaoTaoRepository().LayMaHeDaoTaoTuMaSV(maSVdangXet);
            int maNienKhoa = new NienKhoaRepository().LayMaNienKhoaTuMaSV(maSVdangXet);
            #endregion

            #region Tín chỉ
            #region Chuyên ngành 1
            float soTCBatBuocPhaiHT = tcDauRaRepo.SoTCBatBuocPhaiHTCuaSV(maSVdangXet);
            float soTCTuChonPhaiHT = tcDauRaRepo.SoTCTuChonPhaiHTCuaSV(maSVdangXet);

            float? tongTCDaHT = sinhVienRepo.TinChiTichLuyCua(maSVdangXet);
            float tongTCPhaiHT = tcDauRaRepo.TongTCTLPhaiHTCuaSV(maSVdangXet);
            
            float soTCTichLuy_Thuc = 0;
            float? tcTichLuyBatBuoc = 0;
            List<KhoiCTDT_TC> TCTichLuyTuChonTheoNhom;

            string dsMHBatBuocDaHT = "";
            string dsMHBatBuocChuaHT = "";

            sinhVienRepo.TichLuyBatBuocCua(maSVdangXet, out dsMHBatBuocDaHT, out tcTichLuyBatBuoc, out dsMHBatBuocChuaHT);

            sinhVienRepo.TichLuyTuChonCua(maSVdangXet, out TCTichLuyTuChonTheoNhom, out soTCTichLuy_Thuc);

            ViewData["TongTCDaHT"] = tongTCDaHT;
            ViewData["TongTCPhaiHT"] = tongTCPhaiHT;
            model.tyLetcTichLuyTongQuat = ((tongTCDaHT.Value) / (tongTCPhaiHT) * 100);

            ViewData["SoTCBatBuocDaHT"] = tcTichLuyBatBuoc;
            ViewData["SoTCBatBuocPhaiHT"] = soTCBatBuocPhaiHT;
            model.tyLetcTichLuyBatBuoc = ((tcTichLuyBatBuoc.Value) / (soTCBatBuocPhaiHT) * 100);

            ViewData["HocPhanBatBuocDaHT"] = dsMHBatBuocDaHT;
            ViewData["HocPhanBatBuocChuaHT"] = dsMHBatBuocChuaHT;

            model.tcTuChonTheoNhom = TCTichLuyTuChonTheoNhom;

            ViewData["SoTCTuChonDaHT"] = soTCTichLuy_Thuc;
            ViewData["SoTCTuChonPhaiHT"] = soTCTuChonPhaiHT;
            model.tyLetcTichLuyTuChon = ((soTCTichLuy_Thuc) / (soTCTuChonPhaiHT) * 100);

            #endregion

            #region Chuyên ngành 2
            if (new NganhHoc2Repository().CoHocChuyenNganh2(maSVdangXet))
            {
                float soTCBatBuocPhaiHT_CN2 = tcDauRaRepo.SoTCBatBuocPhaiHTCuaSV_CN2(maSVdangXet);
                float soTCTuChonPhaiHT_CN2 = tcDauRaRepo.SoTCTuChonPhaiHTCuaSV_CN2(maSVdangXet);

                float? tongTCDaHT_CN2 = sinhVienRepo.TinChiTichLuyCN2Cua(maSVdangXet);
                float tongTCPhaiHT_CN2 = tcDauRaRepo.TongTCTLPhaiHTCuaSV_CN2(maSVdangXet);

                float soTCTichLuy_Thuc_CN2 = 0;
                float? tcTichLuyBatBuoc_CN2 = 0;
                List<KhoiCTDT_TC> soTCTichLuyTuChonTheoNhom_CN2;

                string dsMHBatBuocDaHT_CN2 = "";
                string dsMHBatBuocChuaHT_CN2 = "";

                sinhVienRepo.TichLuyBatBuocCN2Cua(maSVdangXet, out dsMHBatBuocDaHT_CN2, out tcTichLuyBatBuoc_CN2, out dsMHBatBuocChuaHT_CN2);

                sinhVienRepo.TichLuyTuChonCN2Cua(maSVdangXet, out soTCTichLuyTuChonTheoNhom_CN2, out soTCTichLuy_Thuc_CN2);

                ViewData["TongTCDaHT_CN2"] = tongTCDaHT_CN2;
                ViewData["TongTCPhaiHT_CN2"] = tongTCPhaiHT_CN2;
                model.tyLetcTichLuyTongQuatCN2 = ((tongTCDaHT_CN2.Value) / (tongTCPhaiHT_CN2) * 100);

                ViewData["SoTCBatBuocDaHT_CN2"] = tcTichLuyBatBuoc_CN2;
                ViewData["SoTCBatBuocPhaiHT_CN2"] = soTCBatBuocPhaiHT_CN2;
                model.tyLetcTichLuyBatBuocCN2 = ((tcTichLuyBatBuoc_CN2.Value) / (soTCBatBuocPhaiHT_CN2) * 100);

                ViewData["HocPhanBatBuocDaHT_CN2"] = dsMHBatBuocDaHT_CN2;
                ViewData["HocPhanBatBuocChuaHT_CN2"] = dsMHBatBuocChuaHT_CN2;

                model.tcTuChonTheoNhom_CN2 = soTCTichLuyTuChonTheoNhom_CN2;

                ViewData["SoTCTuChonDaHT_CN2"] = soTCTichLuy_Thuc_CN2;
                ViewData["SoTCTuChonPhaiHT_CN2"] = soTCTuChonPhaiHT_CN2;
                model.tyLetcTichLuyTuChonCN2 = ((soTCTichLuy_Thuc_CN2) / (soTCTuChonPhaiHT_CN2) * 100);
            }
            #endregion
            #endregion

            #region Toeic
            float diemToeicPhaiHT = chuanDauRaRepo.ChuanToeicCua(maSVdangXet);
            float diemToeicHT = sinhVienRepo.DiemToeicDaHT(maSVdangXet);

            ViewData["DiemToeicDaHT"] = diemToeicHT;
            ViewData["DiemToeicPhaiHT"] = diemToeicPhaiHT;
            model.tyLeToeic = ((diemToeicHT / (diemToeicPhaiHT)) * 100);

            ViewData["HocPhanAVGTDaHT"] = sinhVienRepo.DSMonAVGTDaHT(maSVdangXet);

            #endregion

            #region GDQP
            float soHPGDQPHT = monHocRepo.LayDanhSachGDQPDaHT(maSVdangXet).Count();
            ViewData["DSHocPhanGDQPDaHT"] = sinhVienRepo.DSMonGDQPDaHT(maSVdangXet);
            //ViewData["DSHocPhanGDQPChuaHT"] = sinhVienRepo.DSMonGDQPChuaHT(maSVdangXet);
            model.tyLeGDQP = ((soHPGDQPHT / chuanDauRaRepo.SoHocPhanGDQPDaHTCua(maSVdangXet)) * 100);
            #endregion

            #region GDTC
            float soHPGDTCHT = monHocRepo.LayDanhSachGDTCDaHT(maSVdangXet).Count();
            ViewData["DSHocPhanGDTCDaHT"] = sinhVienRepo.DSMonGDTCDaHT(maSVdangXet);
            model.tyLeGDTC = ((soHPGDTCHT / chuanDauRaRepo.SoHocPhanGDTCDaHTCua(maSVdangXet)) * 100);
            #endregion

            #region Điều kiện Thực tập tốt nghiệp
                      
            model.qdTTTN = qdLVTTTNRepo.LayQuyDinhTTTNMoiNhat(maSVdangXet, 2);
            foreach (var qd in model.qdTTTN)
            {
                qd.count = new int[14];                
                
                qd.dsMHTruoc = monHocRepo.LayDSMHTuChuoiMaMH(qd.CacMonHocTruoc);
                qd.dsMHTienQuyet = monHocRepo.LayDSMHTuChuoiMaMH(qd.CacMonHocTienQuyet);
                if (qd.DTBTLChung != null)
                    qd.count[1]++;
                
                if (qd.SoTinChiTLChung != null)
                    qd.count[2]++;
                
                if (qd.DTBTLKhoiChuyenNganh != null)
                    qd.count[3]++;
                
                if (qd.DTBTLKhoiCoSoNganh != null)
                    qd.count[4]++;
                
                if (qd.DTBTLKhoiNganh != null)
                    qd.count[5]++;
                
                if (qd.DTBTLKhoiCoSoKhoiNganhKhoiChuyenNganh != null)
                    qd.count[6]++;

                if (qd.CacMonHocKhongDuocNo != null)
                    qd.count[7]++;
                
                if (qd.SoTinChiToiDaNo!= null)
                    qd.count[8]++;
                
                if (qd.DiemThucTapTotNghiep != null)
                    qd.count[9]++;
                
                if (qd.DiemTienLuanVan != null)
                    qd.count[10]++;
                
                if (qd.SoLuongSVToiDaDuDKTheoChuyenNganh != null)
                    qd.count[11]++;
                
                if (qd.CacMonHocTruoc != null)
                    qd.count[12]++;
                
                if (qd.CacMonHocTienQuyet != null)
                    qd.count[13]++;

            }
            #endregion

            #region Luận văn tốt nghiệp

            model.qdLV = qdLVTTTNRepo.LayQuyDinhLuanVanMoiNhat(maSVdangXet, 2);
            
            foreach (var qdlv in model.qdLV)
            {
                qdlv.dsMHKhongDuocNo = monHocRepo.LayDSMHTuChuoiMaMH(qdlv.CacMonHocKhongDuocNo);

                if (qdlv.DTBTLChung != null)
                    qdlv.count[1]++;

                if (qdlv.SoTinChiTLChung != null)
                    qdlv.count[2]++;

                if (qdlv.DTBTLKhoiChuyenNganh != null)
                    qdlv.count[3]++;

                if (qdlv.DTBTLKhoiCoSoNganh != null)
                    qdlv.count[4]++;

                if (qdlv.DTBTLKhoiNganh != null)
                    qdlv.count[5]++;

                if (qdlv.DTBTLKhoiCoSoKhoiNganhKhoiChuyenNganh != null)
                    qdlv.count[6]++;

                if (qdlv.CacMonHocKhongDuocNo != null)
                    qdlv.count[7]++;

                if (qdlv.SoTinChiToiDaNo != null)
                    qdlv.count[8]++;

                if (qdlv.DiemThucTapTotNghiep != null)
                    qdlv.count[9]++;

                if (qdlv.DiemTienLuanVan != null)
                    qdlv.count[10]++;

                if (qdlv.SoLuongSVToiDaDuDKTheoChuyenNganh != null)
                    qdlv.count[11]++;

                if (qdlv.CacMonHocTruoc != null)
                    qdlv.count[12]++;

                if (qdlv.CacMonHocTienQuyet != null)
                    qdlv.count[13]++;
            }
            #endregion

            #region Thông tin sinh viên
            model.sinhvien = sinhVienRepo.LaySVTuMaSV(maSVdangXet);

            string datetime = model.sinhvien.NgaySinh.Value.ToShortDateString();            
            string nam = datetime.Split('/')[2];
            string thang = datetime.Split('/')[0];
            string ngay = datetime.Split('/')[1];

            model.sinhvien.ngaysinh = ngay + "-" + thang + "-" + nam;

            if (model.sinhvien.Phai == true)
                model.sinhvien.gioitinh = "Nữ";
            else
                model.sinhvien.gioitinh = "Nam";

            #endregion

            #region Đánh giá rèn luyện đạo đức
            KetQuaHocRepository ketQuaHocRepo = new KetQuaHocRepository();
            var rlList = ketQuaHocRepo.LayTatCaDiemRenLuyen(maSVdangXet);
            model.diemChuanDGRL = chuanDauRaRepo.ChuanDGRL(maNganh, maHeDaoTao, maNienKhoa);
            model.diemRLTungNam = ketQuaHocRepo.TinhDiemRenLuyenNam(rlList);
            model.diemDGRLDDToanKhoa = ketQuaHocRepo.DiemTBRLToanKhoa(model.diemRLTungNam);            
            model.tyLeDGRL = ((float)model.diemDGRLDDToanKhoa / (float)model.diemChuanDGRL) * 100;
            #endregion
            return View(model);
        }
    }
}